home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / GREP / GLOB.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  17KB  |  701 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Guido van Rossum.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)glob.c    5.12 (Berkeley) 6/24/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. /*
  42.  * glob(3) -- a superset of the one defined in POSIX 1003.2.
  43.  *
  44.  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
  45.  *
  46.  * Optional extra services, controlled by flags not defined by POSIX:
  47.  *
  48.  * GLOB_MAGCHAR:
  49.  *    Set in gl_flags if pattern contained a globbing character.
  50.  *
  51.  * gl_matchc:
  52.  *    Number of matches in the current invocation of glob.
  53.  */
  54.  
  55. #if WIN_NT
  56. #include <sys/stat.h>
  57. #include <dirent.h>
  58. #include <limits.h>
  59. #include <unistd.h>
  60. #include "glob.h"
  61. #else
  62. #include <sys/cdefs.h>
  63. #include <sys/param.h>
  64. #include <sys/stat.h>
  65. #include <dirent.h>
  66. #include <glob.h>
  67. #endif
  68. #include <ctype.h>
  69. #include <errno.h>
  70. #include <string.h>
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73.  
  74. #if _POSIX_SOURCE
  75. # define    GLOB_MAGCHAR    0x08    /* pattern had globbing characters */
  76. #endif
  77. #if WIN_NT
  78. # define MAXPATHLEN    PATH_MAX
  79. #endif
  80. #define    DOLLAR        '$'
  81. #define    DOT        '.'
  82. #define    EOS        '\0'
  83. #define    LBRACKET    '['
  84. #define    NOT        '!'
  85. #define    QUESTION    '?'
  86. #define    QUOTE        '\\'
  87. #define    RANGE        '-'
  88. #define    RBRACKET    ']'
  89. #define    SEP        '/'
  90. #define    STAR        '*'
  91. #define    TILDE        '~'
  92. #define    UNDERSCORE    '_'
  93.  
  94. #define    M_QUOTE        0x8000
  95. #define    M_PROTECT    0x4000
  96. #define    M_MASK        0xFFFF
  97. #define    M_ASCII        0x00FF
  98.  
  99. #define    CHAR(c)        ((c)&M_ASCII)
  100. #define    META(c)        ((c)|M_QUOTE)
  101. #define    M_ALL        META('*')
  102. #define    M_END        META(']')
  103. #define    M_NOT        META('!')
  104. #define    M_ONE        META('?')
  105. #define    M_RNG        META('-')
  106. #define    M_SET        META('[')
  107. #define    ismeta(c)    (((c)&M_QUOTE) != 0)
  108.  
  109. #if WIN_NT
  110. typedef unsigned short Char;
  111. typedef unsigned char u_char;
  112. typedef unsigned int u_int;
  113.  
  114. static int     compare (const void *, const void *);
  115. static void     g_Ctoc (Char *, char *);
  116. # if 0
  117. static int     g_lstat (Char *, struct stat *);
  118. # endif
  119. static DIR    *g_opendir (Char *);
  120. static Char    *g_strchr (Char *, int);
  121. static int     g_stat (Char *, struct stat *);
  122. static int     glob1 (Char *, glob_t *);
  123. static int     glob2 (Char *, Char *, Char *, glob_t *);
  124. static int     glob3 (Char *, Char *, Char *, Char *, glob_t *);
  125. static int     globextend (Char *, glob_t *);
  126. static int     match (Char *, Char *, Char *);
  127. #ifdef DEBUG
  128. static void     qprintf (Char *);
  129. #endif
  130. #else
  131. typedef u_short Char;
  132.  
  133. static int     compare __P((const void *, const void *));
  134. static void     g_Ctoc __P((Char *, char *));
  135. # if 0
  136. static int     g_lstat __P((Char *, struct stat *));
  137. # endif
  138. static DIR    *g_opendir __P((Char *));
  139. static Char    *g_strchr __P((Char *, int));
  140. static int     g_stat __P((Char *, struct stat *));
  141. static int     glob1 __P((Char *, glob_t *));
  142. static int     glob2 __P((Char *, Char *, Char *, glob_t *));
  143. static int     glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
  144. static int     globextend __P((Char *, glob_t *));
  145. static int     match __P((Char *, Char *, Char *));
  146. #ifdef DEBUG
  147. static void     qprintf __P((Char *));
  148. #endif
  149. #endif
  150. #if WIN_NT
  151.  
  152. static int    isquotesep;    /* Should SEP be QUOTE? */
  153. #endif
  154.  
  155. /*
  156.  * The main glob() routine: compiles the pattern (optionally processing
  157.  * quotes), calls glob1() to do the real pattern matching, and finally
  158.  * sorts the list (unless unsorted operation is requested).  Returns 0
  159.  * if things went well, nonzero if errors occurred.  It is not an error
  160.  * to find no matches.
  161.  */
  162. int
  163. #if __STDC__
  164. glob (const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
  165. #else
  166. glob(pattern, flags, errfunc, pglob)
  167.     const char *pattern;
  168.     int flags, (*errfunc) __P((char *, int));
  169.     glob_t *pglob;
  170. #endif
  171. {
  172.     const u_char *compilepat, *patnext;
  173.     int c, err, oldpathc;
  174.     Char *bufnext, *bufend, *compilebuf, *qpatnext, patbuf[MAXPATHLEN+1];
  175.  
  176. #if WIN_NT
  177.     isquotesep = (getppid() == (pid_t) 1); /* is parent CMD.EXE? */
  178. #endif
  179.     patnext = (u_char *) pattern;
  180.     if (!(flags & GLOB_APPEND)) {
  181.         pglob->gl_pathc = 0;
  182.         pglob->gl_pathv = NULL;
  183.         if (!(flags & GLOB_DOOFFS))
  184.             pglob->gl_offs = 0;
  185.     }
  186.     pglob->gl_flags = flags & ~GLOB_MAGCHAR;
  187.     pglob->gl_errfunc = errfunc;
  188.     oldpathc = (int) pglob->gl_pathc;
  189.     pglob->gl_matchc = 0;
  190.  
  191.     bufnext = patbuf;
  192.     bufend = bufnext + MAXPATHLEN;
  193.     compilebuf = bufnext;
  194.     compilepat = patnext;
  195.     if (!(flags & GLOB_NOESCAPE)) {
  196.         /* Protect the quoted characters. */
  197.         while (bufnext < bufend && (c = *patnext++) != EOS) 
  198.             if (c == QUOTE) {
  199. #if WIN_NT
  200.                 if (isquotesep)
  201.                     *bufnext++ = (Char) SEP;
  202.                 else {
  203.                     if ((c = *patnext++) == EOS) {
  204.                         c = QUOTE;
  205.                         --patnext;
  206.                     }
  207.                     *bufnext++ = (Char) (c | M_PROTECT);
  208.                 }
  209. #else
  210.                 if ((c = *patnext++) == EOS) {
  211.                     c = QUOTE;
  212.                     --patnext;
  213.                 }
  214.                 *bufnext++ = (Char) (c | M_PROTECT);
  215. #endif
  216.             }
  217.             else
  218.                 *bufnext++ = (Char) c;
  219.     }
  220.     else 
  221.         while (bufnext < bufend && (c = *patnext++) != EOS) 
  222.             *bufnext++ = (Char) c;
  223.     *bufnext = EOS;
  224.  
  225.     bufnext = patbuf;
  226.     qpatnext = patbuf;
  227.     /* We don't need to check for buffer overflow any more. */
  228.     while ((c = *qpatnext++) != EOS) {
  229.         switch (c) {
  230.         case LBRACKET:
  231.             pglob->gl_flags |= GLOB_MAGCHAR;
  232.             c = *qpatnext;
  233.             if (c == NOT)
  234.                 ++qpatnext;
  235.             if (*qpatnext == EOS ||
  236.                 g_strchr(qpatnext+1, RBRACKET) == NULL) {
  237.                 *bufnext++ = LBRACKET;
  238.                 if (c == NOT)
  239.                     --qpatnext;
  240.                 break;
  241.             }
  242.             *bufnext++ = M_SET;
  243.             if (c == NOT)
  244.                 *bufnext++ = M_NOT;
  245.             c = *qpatnext++;
  246.             do {
  247.                 *bufnext++ = (Char) CHAR(c);
  248.                 if (*qpatnext == RANGE &&
  249.                     (c = qpatnext[1]) != RBRACKET) {
  250.                     *bufnext++ = M_RNG;
  251.                     *bufnext++ = (Char) CHAR(c);
  252.                     qpatnext += 2;
  253.                 }
  254.             } while ((c = *qpatnext++) != RBRACKET);
  255.             *bufnext++ = M_END;
  256.             break;
  257.         case QUESTION:
  258.             pglob->gl_flags |= GLOB_MAGCHAR;
  259.             *bufnext++ = M_ONE;
  260.             break;
  261.         case STAR:
  262.             pglob->gl_flags |= GLOB_MAGCHAR;
  263.             *bufnext++ = M_ALL;
  264.             break;
  265.         default:
  266.             *bufnext++ = (Char) CHAR(c);
  267.             break;
  268.         }
  269.     }
  270.     *bufnext = EOS;
  271. #ifdef DEBUG
  272.     qprintf(patbuf);
  273. #endif
  274.  
  275.     if ((err = glob1(patbuf, pglob)) != 0)
  276.         return(err);
  277.  
  278.     if (pglob->gl_matchc == 0 && !(flags & GLOB_NOCHECK))
  279.         return(GLOB_NOMATCH);
  280.  
  281.     if (pglob->gl_pathc == (size_t) oldpathc && (flags & GLOB_NOCHECK)) {
  282. #if WIN_NT
  283.         if (isquotesep || (flags & GLOB_NOESCAPE)) {
  284. #else
  285.         if (flags & GLOB_NOESCAPE) {
  286. #endif
  287.             Char *dp = compilebuf;
  288.             const u_char *sp = compilepat;
  289.             while ((*dp++ = *sp++) != '\0')
  290.                 ;
  291. #if WIN_NT && CONVERT_TO_SLASHES
  292.             for (dp = compilebuf; *dp != '\0'; ++dp)
  293.                 if (*dp == QUOTE)
  294.                     *dp = SEP;
  295. #endif
  296.         }
  297.         else {
  298.             /*
  299.              * Copy pattern, interpreting quotes; this is slightly
  300.              * different than the interpretation of quotes above
  301.              * -- which should prevail?
  302.              */
  303.             while (*compilepat != EOS) {
  304.                 if (*compilepat == QUOTE) {
  305.                     if (*++compilepat == EOS)
  306.                         --compilepat;
  307.                 }
  308.                 *compilebuf++ = (u_char)*compilepat++;
  309.             }
  310.             *compilebuf = EOS;
  311.         }
  312.         return(globextend(patbuf, pglob));
  313.     } else if (!(flags & GLOB_NOSORT)) 
  314.         qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
  315.             pglob->gl_pathc - oldpathc, sizeof(char *), compare);
  316.     return(0);
  317. }
  318.  
  319. static int
  320. #if __STDC__
  321. compare (const void *p, const void *q)
  322. #else
  323. compare(p, q)
  324.     const void *p, *q;
  325. #endif
  326. {
  327.     return(strcmp(*(char **)p, *(char **)q));
  328. }
  329.  
  330. static int
  331. #if __STDC__
  332. glob1 (Char *pattern, glob_t *pglob)
  333. #else
  334. glob1(pattern, pglob)
  335.     Char *pattern;
  336.     glob_t *pglob;
  337. #endif
  338. {
  339.     Char pathbuf[MAXPATHLEN+1];
  340.  
  341.     /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
  342.     if (*pattern == EOS)
  343.         return(0);
  344.     return(glob2(pathbuf, pathbuf, pattern, pglob));
  345. }
  346.  
  347. /*
  348.  * The functions glob2 and glob3 are mutually recursive; there is one level
  349.  * of recursion for each segment in the pattern that contains one or more
  350.  * meta characters.
  351.  */
  352. static int
  353. #if __STDC__
  354. glob2 (Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
  355. #else
  356. glob2(pathbuf, pathend, pattern, pglob)
  357.     Char *pathbuf, *pathend, *pattern;
  358.     glob_t *pglob;
  359. #endif
  360. {
  361.     struct stat sb;
  362.     Char *p, *q;
  363.     int anymeta;
  364.  
  365.     /*
  366.      * Loop over pattern segments until end of pattern or until
  367.      * segment with meta character found.
  368.      */
  369.     for (anymeta = 0;;) {
  370.         if (*pattern == EOS) {        /* End of pattern? */
  371.             *pathend = EOS;
  372.             if (g_stat(pathbuf, &sb))
  373.                 return(0);
  374.         
  375.             if (((pglob->gl_flags & GLOB_MARK) &&
  376. #if WIN_NT
  377.                 pathend[-1] != SEP) && S_ISDIR(sb.st_mode)) {
  378.                 *pathend++ = SEP;
  379. #else
  380.                 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
  381.                 || (S_ISLNK(sb.st_mode) &&
  382.                 (g_stat(pathbuf, &sb) == 0) &&
  383.                 S_ISDIR(sb.st_mode)))) {
  384.                 *pathend++ = SEP;
  385. #endif
  386.                 *pathend = EOS;
  387.             }
  388.             ++pglob->gl_matchc;
  389.             return(globextend(pathbuf, pglob));
  390.         }
  391.  
  392.         /* Find end of next segment, copy tentatively to pathend. */
  393.         q = pathend;
  394.         p = pattern;
  395.         while (*p != EOS && *p != SEP) {
  396.             if (ismeta(*p))
  397.                 anymeta = 1;
  398.             *q++ = *p++;
  399.         }
  400.  
  401.         if (!anymeta) {        /* No expansion, do next segment. */
  402.             pathend = q;
  403.             pattern = p;
  404.             while (*pattern == SEP)
  405.                 *pathend++ = *pattern++;
  406.         } else            /* Need expansion, recurse. */
  407.             return(glob3(pathbuf, pathend, pattern, p, pglob));
  408.     }
  409.     /* NOTREACHED */
  410. }
  411.  
  412. static int
  413. #if __STDC__
  414. glob3 (Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob_t *pglob)
  415. #else
  416. glob3(pathbuf, pathend, pattern, restpattern, pglob)
  417.     Char *pathbuf, *pathend, *pattern, *restpattern;
  418.     glob_t *pglob;
  419. #endif
  420. {
  421.     register struct dirent *dp;
  422.     DIR *dirp;
  423.     int err;
  424.  
  425.     *pathend = EOS;
  426.     errno = 0;
  427.         
  428.     if (!(dirp = g_opendir(pathbuf)))
  429.         /* TODO: don't call for ENOENT or ENOTDIR? */
  430.         if (pglob->gl_errfunc &&
  431.             (*pglob->gl_errfunc)((char *) pathbuf, errno) ||
  432.             (pglob->gl_flags & GLOB_ERR))
  433.             return(GLOB_ABORTED);
  434.         else
  435.             return(0);
  436.  
  437.     err = 0;
  438.  
  439.     /* Search directory for matching names. */
  440.     while ((dp = readdir(dirp)) != NULL) {
  441.         register u_char *sc;
  442.         register Char *dc;
  443.  
  444.         /* Initial DOT must be matched literally. */
  445.         if (dp->d_name[0] == DOT && *pattern != DOT)
  446.             continue;
  447.         for (sc = (u_char *) dp->d_name, dc = pathend; 
  448.              (*dc++ = *sc++) != '\0';)
  449.             ;
  450.         if (!match(pathend, pattern, restpattern)) {
  451.             *pathend = EOS;
  452.             continue;
  453.         }
  454.         err = glob2(pathbuf, --dc, restpattern, pglob);
  455.         if (err)
  456.             break;
  457.     }
  458.  
  459.     /* TODO: check error from readdir? */
  460.     (void)closedir(dirp);
  461.     return(err);
  462. }
  463.  
  464.  
  465. /*
  466.  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
  467.  * add the new item, and update gl_pathc.
  468.  *
  469.  * This assumes the BSD realloc, which only copies the block when its size
  470.  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
  471.  * behavior.
  472.  *
  473.  * Return 0 if new item added, error code if memory couldn't be allocated.
  474.  *
  475.  * Invariant of the glob_t structure:
  476.  *    Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
  477.  *    gl_pathv points to (gl_offs + gl_pathc + 1) items.
  478.  */
  479. static int
  480. #if __STDC__
  481. globextend (Char *path, glob_t *pglob)
  482. #else
  483. globextend(path, pglob)
  484.     Char *path;
  485.     glob_t *pglob;
  486. #endif
  487. {
  488.     register char **pathv;
  489.     register int i;
  490.     u_int newsize;
  491.     char *copy;
  492.     Char *p;
  493.  
  494.     newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
  495.     pathv = (char **)realloc((char *)pglob->gl_pathv, newsize);
  496.     if (pathv == NULL)
  497.         return(GLOB_NOSPACE);
  498.  
  499.     if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
  500.         /* first time around -- clear initial gl_offs items */
  501.         pathv += pglob->gl_offs;
  502.         for (i = pglob->gl_offs; --i >= 0; )
  503.             *--pathv = NULL;
  504.     }
  505.     pglob->gl_pathv = pathv;
  506.  
  507.     for (p = path; *p++;);
  508.     if ((copy = malloc(p - path)) != NULL) {
  509. #if WIN_NT && CONVERT_TO_BACKSLASHES
  510.         char *c;
  511.  
  512.         g_Ctoc(path, copy);
  513.         for (c = copy; *c++;)
  514.             if (isquotesep && *c == SEP)
  515.                 *c = QUOTE;
  516. #else
  517.         g_Ctoc(path, copy);
  518. #endif
  519.         pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
  520.     }
  521.     pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
  522.     return(copy == NULL ? GLOB_NOSPACE : 0);
  523. }
  524.  
  525.  
  526. /*
  527.  * pattern matching function for filenames.  Each occurrence of the *
  528.  * pattern causes a recursion level.
  529.  */
  530. static int
  531. #if __STDC__
  532. match (register Char *name, register Char *pat, register Char *patend)
  533. #else
  534. match(name, pat, patend)
  535.     register Char *name, *pat, *patend;
  536. #endif
  537. {
  538.     int ok, negate_range;
  539.     Char c, k;
  540.  
  541.     while (pat < patend) {
  542.         c = *pat++;
  543.         switch (c & M_MASK) {
  544.         case M_ALL:
  545.             if (pat == patend)
  546.                 return(1);
  547.             for (; *name != EOS; ++name)
  548.                 if (match(name, pat, patend))
  549.                     return(1);
  550.             return(0);
  551.         case M_ONE:
  552.             if (*name++ == EOS)
  553.                 return(0);
  554.             break;
  555.         case M_SET:
  556.             ok = 0;
  557.             k = *name++;
  558.             if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0)
  559.                 ++pat;
  560.             while (((c = *pat++) & M_MASK) != M_END)
  561.                 if ((*pat & M_MASK) == M_RNG) {
  562.                     if (c <= k && k <= pat[1])
  563.                         ok = 1;
  564.                     pat += 2;
  565.                 } else if (c == k)
  566.                     ok = 1;
  567.             if (ok == negate_range)
  568.                 return(0);
  569.             break;
  570.         default:
  571.             if (*name++ != c)
  572.                 return(0);
  573.             break;
  574.         }
  575.     }
  576.     return(*name == EOS);
  577. }
  578.  
  579. /* Free allocated data belonging to a glob_t structure. */
  580. void
  581. #if __STDC__
  582. globfree (glob_t *pglob)
  583. #else
  584. globfree(pglob)
  585.     glob_t *pglob;
  586. #endif
  587. {
  588.     register int i;
  589.     register char **pp;
  590.  
  591.     if (pglob->gl_pathv != NULL) {
  592.         pp = pglob->gl_pathv + pglob->gl_offs;
  593.         for (i = pglob->gl_pathc; i--; ++pp)
  594.             if (*pp)
  595.                 free(*pp);
  596.         free(pglob->gl_pathv);
  597.     }
  598. }
  599.  
  600. static DIR *
  601. #if __STDC__
  602. g_opendir (register Char *str)
  603. #else
  604. g_opendir(str)
  605.     register Char *str;
  606. #endif
  607. {
  608.     char buf[MAXPATHLEN];
  609.  
  610.     if (!*str)
  611.         return(opendir("."));
  612.     g_Ctoc(str, buf);
  613.     return(opendir(buf));
  614. }
  615. #if 0
  616.  
  617. static int
  618. #if __STDC__
  619. g_lstat (register Char *fn, struct stat *sb)
  620. #else
  621. g_lstat(fn, sb)
  622.     register Char *fn;
  623.     struct stat *sb;
  624. #endif
  625. {
  626.     char buf[MAXPATHLEN];
  627.  
  628.     g_Ctoc(fn, buf);
  629.     return(lstat(buf, sb));
  630. }
  631. #endif
  632.  
  633. static int
  634. #if __STDC__
  635. g_stat (register Char *fn, struct stat *sb)
  636. #else
  637. g_stat(fn, sb)
  638.     register Char *fn;
  639.     struct stat *sb;
  640. #endif
  641. {
  642.     char buf[MAXPATHLEN];
  643.  
  644.     g_Ctoc(fn, buf);
  645.     return(stat(buf, sb));
  646. }
  647.  
  648. static Char *
  649. #if __STDC__
  650. g_strchr (Char *str, int ch)
  651. #else
  652. g_strchr(str, ch)
  653.     Char *str;
  654.     int ch;
  655. #endif
  656. {
  657.     do {
  658.         if (*str == (Char) ch)
  659.             return (str);
  660.     } while (*str++);
  661.     return (NULL);
  662. }
  663.  
  664. static void
  665. #if __STDC__
  666. g_Ctoc (register Char *str, char *buf)
  667. #else
  668. g_Ctoc(str, buf)
  669.     register Char *str;
  670.     char *buf;
  671. #endif
  672. {
  673.     register char *dc;
  674.  
  675.     for (dc = buf; (*dc++ = (char) *str++) != '\0';)
  676.         ;
  677. }
  678.  
  679. #ifdef DEBUG
  680. static void 
  681. #if __STDC__
  682. qprintf (register Char *s)
  683. #else
  684. qprintf(s)
  685.     register Char *s;
  686. #endif
  687. {
  688.     register Char *p;
  689.  
  690.     for (p = s; *p; p++)
  691.         (void)printf("%c", *p & 0xff);
  692.     (void)printf("\n");
  693.     for (p = s; *p; p++)
  694.         (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
  695.     (void)printf("\n");
  696.     for (p = s; *p; p++)
  697.         (void)printf("%c", *p & M_META ? '_' : ' ');
  698.     (void)printf("\n");
  699. }
  700. #endif
  701.